//
//  appendAttributedStringWithEase.swift
//  Ysoftware
//
//  Created by Yaroslav Erohin on 16.07.15.
//  Copyright (c) 2015 Yaroslav Erohin. All rights reserved.
//

import UIKit

extension NSMutableAttributedString {

    /// Appends an attributed string with given attributes.
    ///
    /// - parameter string: A string you want to add.
    /// - parameter attributes: The attributes for the new attributed string.
    public func append(_ string:String, withAttributes attributes:[NSAttributedString.Key : Any]?) {
        self.append(NSAttributedString(string: string, attributes: attributes))
    }

    // MARK: - Fonts

    /// Appends an attributed string with a dynamic font.
    ///
    /// - parameter string: A string you want to add.
    /// - parameter style: A text style for dynamic font (ex. UIFontTextStyleBody).
    public func append(_ string:String, withStyle style:String) {
        self.append(string, withFont: UIFont.preferredFont(forTextStyle: UIFont.TextStyle(rawValue: style)))
    }

    /// Appends an attributed string with a given font.
    ///
    /// - parameter string: A string you want to add.
    /// - parameter font: An instance of UIFont.
    public func append(_ string:String, withFont font:UIFont) {
        self.append(string, withAttributes: [NSAttributedString.Key.font:font])
    }

    // MARK: - Colors

    /// Appends an attributed string with a given color and a font.
    ///
    /// - parameter string: A string you want to add.
    /// - parameter color: An instance of UIColor.
    /// - parameter font: An instance of UIFont.
    func append(_ string:String, withColor color:UIColor, andFont font:UIFont) {
        self.append(string, withAttributes: [NSAttributedString.Key.font:font, NSAttributedString.Key.foregroundColor:color])
    }

    /// Appends an attributed string with a given color and a dynamic font.
    ///
    /// - parameter string: A string you want to add.
    /// - parameter color: An instance of UIColor.
    /// - parameter style: A text style for dynamic font (ex. UIFontTextStyleBody).
    public func append(_ string:String, withColor color:UIColor, andStyle style:String) {
        self.append(string, withColor: color, andFont: UIFont.preferredFont(forTextStyle: UIFont.TextStyle(rawValue: style)))
    }

    // MARK: - Underline

    /// Appends an attributed string with underlined text of a given color and a dynamic font.
    ///
    /// - parameter string: A string you want to add.
    /// - parameter font: An instance of UIFont.
    /// - parameter underlineStyle: A style for the underline.
    public func append(_ string:String, withFont font:UIFont, andUnderlineStyle underline:NSUnderlineStyle) {
        self.append(string, withAttributes: [NSAttributedString.Key.font:font, NSAttributedString.Key.underlineStyle:underline])
    }

    /// Appends an attributed string with underlined text of a given color and a dynamic font.
    ///
    /// - parameter string: A string you want to add.
    /// - parameter color: An instance of UIColor.
    /// - parameter font: An instance of UIFont.
    /// - parameter underlineStyle: A style for the underline.
    public func append(_ string:String, withColor color:UIColor, font:UIFont, andUnderlineStyle underline:NSUnderlineStyle) {
        self.append(string, withAttributes: [NSAttributedString.Key.font:font, NSAttributedString.Key.foregroundColor:color, NSAttributedString.Key.underlineStyle:underline])
    }

    /// Appends an attributed string with underlined text of a given color and a dynamic font.
    ///
    /// - parameter string: A string you want to add.
    /// - parameter color: An instance of UIColor.
    /// - parameter style: A text style for dynamic font (ex. UIFontTextStyleBody).
    /// - parameter underlineStyle: A style for the underline.
    public func append(_ string:String, withColor color:UIColor, style:String, andUnderlineStyle underline:NSUnderlineStyle) {
        self.append(string, withColor: color, font: UIFont.preferredFont(forTextStyle: UIFont.TextStyle(rawValue: style)), andUnderlineStyle: underline)
    }

    // MARK: - Strike Through

    /// Appends an attributed string with a given color and a dynamic font with a strike through line.
    ///
    /// - parameter string: A string you want to add.
    /// - parameter font: An instance of UIFont.
    /// - parameter underlineStyle: A style for the strike through line.
    public func append(_ string:String, withfont font:UIFont, andStrikeThroughStyle strike:NSUnderlineStyle) {
        self.append(string, withAttributes: [NSAttributedString.Key.font:font, NSAttributedString.Key.strikethroughStyle:strike])
    }

    /// Appends an attributed string with a given color and a dynamic font with a strike through line.
    ///
    /// - parameter string: A string you want to add.
    /// - parameter color: An instance of UIColor.
    /// - parameter font: An instance of UIFont.
    /// - parameter underlineStyle: A style for the strike through line.
    public func append(_ string:String, withColor color:UIColor, font:UIFont, andStrikeThroughStyle strike:NSUnderlineStyle) {
        self.append(string, withAttributes: [NSAttributedString.Key.font:font, NSAttributedString.Key.foregroundColor:color, NSAttributedString.Key.strikethroughStyle:strike])
    }

    // MARK: - Dynamic Fonts

    /// Appends an attributed string with a dynamic font of style Subheadline.
    ///
    /// - parameter string: A string you want to add.
    public func appendSubheadline(_ string:String) {
        self.append(string, withFont: UIFont.preferredFont(forTextStyle: UIFont.TextStyle.subheadline))
    }

    /// Appends an attributed string with a dynamic font of style Caption1.
    ///
    /// - parameter string: A string you want to add.
    public func appendCaption1(_ string:String) {
        self.append(string, withFont: UIFont.preferredFont(forTextStyle: UIFont.TextStyle.caption1))
    }

    /// Appends an attributed string with a dynamic font of style Caption2.
    ///
    /// - parameter string: A string you want to add.
    public func appendCaption2(_ string:String) {
        self.append(string, withFont: UIFont.preferredFont(forTextStyle: UIFont.TextStyle.caption2))
    }

    /// Appends an attributed string with a dynamic font of style Headline.
    ///
    /// - parameter string: A string you want to add.
    public func appendHeadline(_ string:String) {
        self.append(string, withFont: UIFont.preferredFont(forTextStyle: UIFont.TextStyle.headline))
    }

    /// Appends an attributed string with a dynamic font of style Footnote.
    ///
    /// - parameter string: A string you want to add.
    public func appendFootnote(_ string:String) {
        self.append(string, withFont: UIFont.preferredFont(forTextStyle: UIFont.TextStyle.footnote))
    }

    /// Appends an attributed string with a dynamic font of style Body.
    ///
    /// - parameter string: A string you want to add.
    public func appendBody(_ string:String) {
        self.append(string, withFont: UIFont.preferredFont(forTextStyle: UIFont.TextStyle.body))
    }
    
}
